package vip.xiaonuo.facedoor.modular.listener;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import vip.xiaonuo.facedoor.modular.device.entity.Device;
import vip.xiaonuo.facedoor.modular.device.entity.DeviceCategory;
import vip.xiaonuo.facedoor.modular.device.service.DeviceService;
import vip.xiaonuo.facedoor.modular.device.service.DeviceSync;
import vip.xiaonuo.facedoor.modular.devicebase.entity.DeviceBase;
import vip.xiaonuo.facedoor.modular.devicebase.service.DeviceBaseService;

import javax.annotation.Resource;
import java.util.Optional;

@Service("FacedoorDeviceSyncService.class")
public class FacedoorDeviceSyncService implements DeviceSync<DeviceBase> {

    @Resource
    private DeviceBaseService deviceBaseService;

    @Resource
    private DeviceService deviceService;

    @Override
    public Long add(Object obj) {
        try {
            DeviceBase deviceBase = (DeviceBase) obj;
            Device device = new Device();
            this.createDevice(device, deviceBase);
            deviceService.save(device);
            return device.getId();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0L;
    }

    @Override
    public Long update(Object obj) {
        try {
            DeviceBase deviceBase = (DeviceBase) obj;
            if (deviceBase == null) {
                return 0L;
            }

            if (deviceBase.getDeviceId() == null) {
                return this.add(obj);
            } else {
                Device device = deviceService.getById(deviceBase.getDeviceId());
                if (device == null) {
                    return this.add(obj);
                } else {
                    this.createDevice(device, deviceBase);
                    deviceService.updateById(device);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0L;
    }

    @Override
    public void delete(Object obj) {
        DeviceBase deviceBase = (DeviceBase) obj;
        if (deviceBase != null && deviceBase.getDeviceId() != null) {
            Device device = deviceService.getById(deviceBase.getDeviceId());
            if (device != null) {
                deviceService.removeById(device.getId());
            }
        }
    }

    @Override
    public void saveRelate(Long id, DeviceBase deviceBase) {
        if (id != null && id > 0) {
            deviceBase.setDeviceId(id);
            deviceBaseService.updateById(deviceBase);
        }
    }

    private void createDevice(Device device, DeviceBase deviceBase) {
        device.setDeviceNo(deviceBase.getId().toString());
        device.setSnCode(deviceBase.getRegistrationCode());
        device.setName(deviceBase.getName());
        device.setType(DeviceCategory.MAIN_TYPE_DOOR);
        device.setSubType(deviceBase.getDeviceType() != null ? Integer.parseInt(deviceBase.getDeviceType()) : null);
        device.setInsLocation(deviceBase.getAddress());
        device.setNetAddress(Optional.ofNullable(deviceBase.getCabinetIp()).orElse("").concat(":").concat(Optional.ofNullable(deviceBase.getCabinetPort()).orElse("")));
        device.setStatus(StringUtils.isBlank(deviceBase.getDeviceState())?2:Integer.parseInt(deviceBase.getDeviceState()));
    }
}
